package com.outfit.job.handler;

import com.outfit.job.util.OkHttpUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * HttpJobHandler
 * @author ming
 * @date 2019-08-30 15:41
 * <p>company: 杭州行装网络技术有限公司</p>
 * <p>修改履历：</p>
 */
@JobHandler(value = "httpJobHandler")
@Component
public class HttpJobHandler extends IJobHandler {

    private static final Logger logger = LoggerFactory.getLogger(HttpJobHandler.class);

    @Autowired
    private OkHttpClient okHttpClient;

    /**
     * 执行get请求
     *
     * @param param 执行参数  https://fanyi.baidu.com/#en/zh/ming
     * @return 执行结果
     * @throws Exception 异常信息
     */
    @Override
    public ReturnT<String> execute(String param) throws Exception {
        if(StringUtils.isEmpty(param)){
            return new ReturnT<>(ReturnT.FAIL_CODE,"执行参数为空");
        }
        String result = OkHttpUtil.get(param, null);
        if(result == null){
            return FAIL;
        }
        logger.info("执行成功！\n\t执行参数：{}\n\t执行结果：{}",param,result);
        return SUCCESS;
    }
}
